package com.doumuxie.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
/**
 * 开启注解
 */
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {



    /**
     * 密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    /**
     * 配置安全拦截机制
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable()
                .authorizeRequests()
                // 访问/r/r1 必须 要 有 r1 权限
//                .antMatchers("/r/r1").hasAnyAuthority("r1")
//                // 访问/r/r2 必须 要 有 /r/r2 权限
//                .antMatchers("/r/r2").hasAnyAuthority("r2")
                // 所有符合条件的请求必须有权限
                .antMatchers("/r/**").authenticated()
                // 其他请求放行
                .anyRequest().permitAll()
                .and()
                // 允许表单登录
                .formLogin()
                // 自定义登录页面
                .loginPage("/index.html")
                // 后台登录地址
                .loginProcessingUrl("/login")
                // 登录成功跳转地址
                .successForwardUrl("/loginSuccess");
    }
}
